package com.tuanzi.loan.web.bean.templateConf;

import com.tuanzi.loan.business.entity.templateConf.LoanApiModelItembasisEntity;
import com.tuanzi.loan.business.entity.templateConf.LoanApiModelProjectEntity;
import com.tuanzi.loan.business.entity.templateConf.LoanApiModelTypeEntity;
import com.tuanzi.loan.business.service.templateConf.TemplateItemService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class TemplateItemInfoBean extends JSFInfoBean<LoanApiModelItembasisEntity> {

    private static final long serialVersionUID = -2152778554108785342L;

    @Autowired
    private TemplateItemService templateItemService;

    private List<LoanApiModelProjectEntity> modelProjectList;
    private List<LoanApiModelTypeEntity> modelTypeList;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = templateItemService.findOne(NumberUtils.toLong(id));
        } else {
            $ = new LoanApiModelItembasisEntity();
            changeStatusToAdd();
        }
        modelProjectList = templateItemService.getModelProjectList();
        modelTypeList = templateItemService.getModelTypeList();
    }

    public void onSave() {
        $ = templateItemService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }

}
